<?php


namespace app\tt\controller;


use app\common\model\MiniApp;
use app\common\model\Money;
use app\common\model\ProductCps;
use EasyWeChat\Factory;
use think\Controller;
use think\Db;
use think\facade\Config;
use think\facade\Request;

class Pay extends Controller
{
    protected $middleware = [
        'app\http\middleware\checkTTUserToken::class'
    ];

    public function pay()
    {
        $user_id = Request::param('user_id');
        $product_id = Request::param('product_id');

        $user = \app\user\model\TtUser::get($user_id);

        $price=ProductCps::where('id',$product_id)->value('price');
        $config = Config::get("miniprogram.wechat_pay");

//        $config['app_id']='wxd45903748a718631';

        $payment = Factory::payment($config);
        $jssdk = $payment->jssdk;

        $time=time();
        $result = $payment->order->unify([
            'body' => '查看测试结果',
            'out_trade_no' => $time,
            'total_fee' => $price*100,
            'notify_url' => 'https://kuaitui.ilanchong.cn/user/vip/payCallBack', // 支付结果通知网址，如果不设置则会使用配置里的默认地址
            'trade_type' => 'JSAPI',        // 请对应换成你的支付方式对应的值类型
            "attach" => json_encode([
                'user_id'=>$user_id,
            ]),        //附加参数，会在回调中原样返回
            'openid' => $user->openid,
        ]);

        $config = $jssdk->bridgeConfig($result['prepay_id'], false); // 返回数组

        $this->result($config, 200);

    }

    //支付后加钱
    public  function addMoney ()
    {
        $user_id = Request::param('user_id');
        $product_id = Request::param('product_id');
        $promote_user_id=Request::param('promote_user_id',0);       //推广者id

        $price=ProductCps::where('id',$product_id)->value('price');

        //cps测评的详细信息
        $mini_app_info=MiniApp::where('code','test_cps')->find();
        $promote_user_info= \app\common\model\User::where('id',$promote_user_id)->find();

        //推广者不存在的话，就直接不加钱
        if(!$promote_user_info){
            $this->result('',200,'没有推广者');
        }

        //转化成数字
       $price=$price*1;

       //给推广者加钱
        if($price>0){
            //推广者是vip的时候
            if($promote_user_info->vip==1){
                $res=Money::addCanTakeOutMoney($promote_user_id,$price*$mini_app_info->vip_rate);
            }
            //推广者不是vip的时候
            if($promote_user_info->vip==0){
                $res=Money::addCanTakeOutMoney($promote_user_id,$price*$mini_app_info->user_rate);
            }

            //存在上级的时候，给上级加钱
            if($promote_user_info->lv_1_id){
                $lv_1_info=\app\common\model\User::get($promote_user_info->lv_1_id);
                //给一级分销加钱
                if($lv_1_info->vip==1){
                    $res=Money::addCanTakeOutMoney($promote_user_info->lv_1_id,$price*$mini_app_info->lv_1_rate);
                }
            }

            //存在上上级的时候，给上上级加钱
            if($promote_user_info->lv_2_id){
                $lv_2_info=\app\common\model\User::get($promote_user_info->lv_2_id);
                if($lv_2_info->vip==1){
                    $res=Money::addCanTakeOutMoney($promote_user_info->lv_2_id,$price*$mini_app_info->lv_2_rate);
                }
            }
        }

        $this->result('',200,'成功');
    }

}
